package cn.listener;

import cn.TCSP;
import cn.util.TCUtil;
import cn.wetcland.task.DataAutoSaveTask;
import cn.wetcland.task.GlobalMarketUpdateTask;
import cn.wetcland.task.WorldResetTask;
import com.tripleying.qwq.LocaleLanguageAPI.LocaleLanguageAPI;
import de.slikey.effectlib.EffectManager;
import de.slikey.effectlib.effect.AnimatedBallEffect;
import de.slikey.effectlib.effect.AtomEffect;
import de.slikey.effectlib.effect.DragonEffect;
import de.slikey.effectlib.effect.WaveEffect;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.event.server.ServerLoadEvent;

import java.io.File;

public class ServerEventListener implements Listener {

    @EventHandler
    public void serverListPingEvent(ServerListPingEvent event) {
        event.setMotd(TCUtil.randomServerMotdStr());
    }

    @EventHandler
    public void serverLoadEvent(ServerLoadEvent event) {
        Bukkit.broadcastMessage("§b服务器已加载");
        Bukkit.getScheduler().runTaskLater(TCSP.getTcsp(), () -> {
            TCSP.signInYaml = YamlConfiguration.loadConfiguration(new File("PlayerData/SignIn.yml"));
            TCSP.basicYaml = YamlConfiguration.loadConfiguration(new File("PlayerData/Basic.yml"));
            TCSP.serverBasicYaml = YamlConfiguration.loadConfiguration(new File("WetclandServer/serverBasic.yml"));
            // 开启任务
            new GlobalMarketUpdateTask().runTask();
            new WorldResetTask().runTask();
            new DataAutoSaveTask().runTask();

            LocaleLanguageAPI.initial();
            TCSP.setEffectLib(new EffectManager(Bukkit.getPluginManager().getPlugin("EffectLib")));
            TCUtil.setServerWorldsGameRule();
            TCSP.mainScoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
            if (TCUtil.getLocationByNameInYaml("mainHall") == null) {
                TCUtil.saveLocationInYaml("mainHall", Bukkit.getWorld("world").getSpawnLocation());
            }
            // 主城原子特效 永不停止
            Location mainHallAtomEffectsLoc = TCUtil.getLocationByNameInYaml("mainHallAtomEffects");
            if (mainHallAtomEffectsLoc != null) {
                AtomEffect atomEffect = new AtomEffect(TCSP.getEffectLib());
                atomEffect.setLocation(mainHallAtomEffectsLoc);
                atomEffect.infinite();
                atomEffect.start();
            } else System.out.println("mainHallAtomEffects坐标不存在，未开启该特效展示");
            // 主城龙特效 永不停止
            Location mainHallWaveEffectsLoc = TCUtil.getLocationByNameInYaml("mainHallDragonEffectEffects");
            if (mainHallWaveEffectsLoc != null) {
                DragonEffect dragonEffect = new DragonEffect(TCSP.getEffectLib());
                dragonEffect.setLocation(mainHallWaveEffectsLoc);
                dragonEffect.infinite();
                dragonEffect.start();
            } else System.out.println("mainHallDragonEffectEffects坐标不存在，未开启该特效展示");
            // DNA旋转特效 永不停止
            Location mainHallAnimatedBallEffectsLoc = TCUtil.getLocationByNameInYaml("mainHallAnimatedBallEffects");
            if (mainHallAnimatedBallEffectsLoc != null) {
                AnimatedBallEffect animatedBallEffect = new AnimatedBallEffect(TCSP.getEffectLib());
                animatedBallEffect.setLocation(mainHallWaveEffectsLoc);
                animatedBallEffect.infinite();
                animatedBallEffect.start();
            } else System.out.println("mainHallAnimatedBallEffects坐标不存在，未开启该特效展示");
            // 重载HD插件
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "hd reload");
        }, 20);
    }

}
